package com.yuanfeng.authserver.controller;

import com.yuanfeng.authserver.service.LoginServcie;
import com.yuanfeng.commoms.annnotation.AccessLimit;
import com.yuanfeng.commoms.annnotation.RepeatClick;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private LoginServcie loginServcie;

    /**
     * 账号密码登录
     * @param user
     * @return
     */
    @RepeatClick
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody UserInfoVO user) throws IOException {
        return loginServcie.login(user);
    }


    /**
     * 手机验证码登录
     * @param paraMap
     * @return
     */
    @RepeatClick
    @RequestMapping( "/user/codelogin")
    public ResponseResult codelogin(@RequestBody Map<String, String> paraMap) throws IOException {

        return loginServcie.codelogin(paraMap);
    }

    /**
     * 退出登录
     * @param user
     * @return
     */

    @PostMapping("/user/loginOut")
    public ResponseResult loginOut(@RequestBody UserInfoVO user){
        return loginServcie.logout(user);
    }


    /**
     * 发送验证码
     * @param paraMap
     * @return
     */
    @PostMapping("/register/sjcheckphoneandsend")
    public ResponseResult sjCheckPhoneAndSend(@RequestBody Map<String, String> paraMap) throws IOException {

        ResponseResult result=loginServcie.sendVerificationCode(paraMap);

        return result;
    }

    /**
     * 平台获取图片验证码
     */
    @PostMapping("/platform/userinfo/getVerify")
    public ResponseResult getVerify() {
        return loginServcie.getVerify();
    }

    /**
     * 商家获取图片验证码
     */
    @PostMapping("/shop/getShopVerify")
    public ResponseResult getShopVerify() {
        return loginServcie.getShopVerify();
    }

    /**
     * 商家获取图片验证码
     */
    @PostMapping("/shop/getSupplierVerify")
    public ResponseResult getSupplierVerify() {
        return loginServcie.getSupplierVerify();
    }

    /**
     * 用户注销
     */
    @RequestMapping(value = "/user/logout" ,method = RequestMethod.POST)
    public ResponseResult userLogOut(@RequestBody Map<String, String> paraMap) {
        String token = paraMap.get("token");
        RedisUtil.remove(YFConfig.YF_USER_TOKEN_USER + token);
        return ResponseResult.success();
    }

    /**
     * 生成验证码
     */
    @RequestMapping(value = "/getVerify" ,method = RequestMethod.POST)
    public ResponseResult getVerify2() {
        ResponseResult result = loginServcie.getVerify2();
        return result;
    }

    /**
     * 生成手机验证码2
     *
     * @param map
     * @return
     * @throws IOException
     */
    @RequestMapping("/verifiPhoneCode2")
    public ResponseResult verifiPhoneCode2(@RequestBody Map<String, Object> map) {
        ResponseResult rr = loginServcie.verifiPhoneCode2(map);
        return rr;
    }

    /**
     * app 微信授权登录
     * @param paraMap
     * @return
     * @throws IOException
     */
    @RequestMapping( "/weixin/uniappLogin")
    public Map<String, Object> uniappLogin(@RequestBody Map<String, String> paraMap) throws IOException {

        return  loginServcie.uniappLogin(paraMap);
    }
    /**
     * app 微信授权注册
     * @param paraMap
     * @return
     * @throws IOException
     */
    @RequestMapping( "/weixin/uniappRegister")
    public ResponseResult uniappRegister(@RequestBody Map<String, String> paraMap) throws IOException, TransactionException {

        return  loginServcie.uniappRegister(paraMap);
    }
    /**
     * 微信小程序 授权登录
     */
    @RequestMapping(value = "/weixin/code2Session")
    @ResponseBody
    @AccessLimit()//防止恶意请求
    public Map<String, Object> code2Session(@RequestBody Map<String, String> paraMap, HttpServletRequest request, HttpServletResponse response) throws IOException{
        return  loginServcie.code2Session(paraMap,request,response);
    }
    /**
     * 微信小程序 授权注册
     */
    @GlobalTransactional
    @PostMapping(value = "/weixin/codelogin")
    public ResponseResult weixinCodelogin(@RequestBody Map<String, String> paraMap)
            throws IOException, TransactionException {
        return  loginServcie.weixinCodelogin(paraMap);
    }
    /**
     * 微信小程序 获取微信小程序code
     */
    @RequestMapping(value = "/weixin/code2SessionopenId")
    @ResponseBody
    @AccessLimit()//防止恶意请求
    public ResponseResult code2SessionopenId(@RequestBody Map<String, String> paraMap) throws IOException{
        return  loginServcie.code2SessionopenId(paraMap);
    }
    /**
     * 查询当前用户 微信小程序是否允许自动登录
     */
    @PostMapping(value = "/weixin/queryAutoLogin")
    public Map<String, Object> queryAutoLogin(@RequestBody Map<String, String> paraMap) throws IOException {
        return loginServcie.queryAutoLogin(paraMap);
    }
    /**
     * 小程序支付前 授权
     */
    @PostMapping("/weixin/paymentAuthorization")
    public Map<String, Object> paymentAuthorization(@RequestBody Map<String, String> paraMap) throws IOException {
        return loginServcie.paymentAuthorization(paraMap);
    }
    /**
     * 微信小程序退出
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/weixin/out")
    public ResponseResult weixinOut(@RequestBody Map<String, String> paraMap) throws IOException {
        return loginServcie.weixinOut(paraMap);
    }

    /**
     * 注销账号
     * @param paraMap
     * @return
     */
    @PostMapping("/h5/userLogoff")
    public ResponseResult userLogoff(@RequestBody Map<String, String> paraMap) {
        return loginServcie.userLogoff(paraMap);
    }
}
